﻿using System;
using System.Data;
using System.Text;
using System.Windows.Markup;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices;
using Petra.Gis.DataServices.Metadata;
using Petra.Gis.DataServices.GDO.Sqlserver;
using Petra.Gis.DataServices.SHP;

namespace testDataServices
{
  public class Grid
  {


    public static void testRastry()
    {


      // prepare and open connection to spatial database
      // mssql server
      GdoSqlConnection c = new GdoSqlConnection()
      {
        ConnectionString =
        @"Data Source=localhost; 
         Initial Catalog=TEST_RASTRY;
				 Integrated Security = True;
         MultipleActiveResultSets=True"
      };

      c.Open();

      // try connection xaml
      string outputConn = XamlWriter.Save(c);
      object conn = XamlReader.Parse(outputConn);

      // prepare command
      GdoSqlCommand cmd = new GdoSqlCommand();
      cmd.Connection = c;
      // this first to get smooth performance
      cmd.CommandType = System.Data.CommandType.Text;
      // set valid command now command
      //cmd.CommandText = "select * from [DS_IL].[dbo].[F_A_Circles_polyg]";
      cmd.CommandText = "select * from [TEST_RASTRY].[dbo].[GR_PORS]";
      // test spatial schema
      //c.GetSpatialSchema(SpatialSchemaTables.Features, new string[] { "DS_IL", "dbo", "F_A_Circles_polyg", null, null, null, null, null, null, null });
      c.GetSpatialSchema(SpatialSchemaTables.Features, new string[] { "TEST_RASTRY", "dbo", "GR_PORS", null, null, null, null, null, null, null });
      // another test for spatial schema
      //DataTable fileds = c.GetSpatialSchema(SpatialSchemaTables.Fields, new string[] { "DS_IL", "dbo", "F_A_Circles_polyg", null, null, null, null });
      DataTable fileds = c.GetSpatialSchema(SpatialSchemaTables.Fields, new string[] { "TEST_RASTRY", "dbo", "GR_PORS", null, null, null, null });

      //// spatial filter set holds spatial filters for command
      //SpatialFilterSet sf = new SpatialFilterSet();
      //sf.Add(fileds.Rows[0]["FIELD_NAME"].ToString(), new BoundingBox(-20, -20, 20, 20));

      //// create parameter with special name
      //GdoSqlParameter tmpParam = cmd.CreateParameter();
      //tmpParam.ParameterName = "Petra.Gis.DataServices.FeatureSpatialFilter";
      //tmpParam.Value = sf;
      //cmd.Parameters.Add(tmpParam);

      GdoSqlDataReader rd = cmd.ExecuteReader();

      rd.Read();
      object g = rd.GetValue(rd.GetOrdinal("Geometry"));

      //BitmapSource bmp = ((Grid)g).Data.Frames[0].Blocks[0].Source;


      FeatureMetadata s = cmd.Metadata; //Metadata;

      string output = XamlWriter.Save(cmd);

    }


  }
}
